
import Vue from 'vue'
function DataTransfer (data) {
    if (!(this instanceof DataTransfer)) {
        return new DataTransfer(data, null, null)
    }
}

DataTransfer.treeToArray = function (data, parent, level, expandedAll) {
    let tmp = []
    Array.from(data).forEach(function (record) {
        if (record._expanded === undefined) {
            Vue.set(record, '_expanded', expandedAll)
        }
        if (record._expanded2 === undefined) {
            Vue.set(record, '_expanded2', expandedAll)
        }
        if (parent) {
            Vue.set(record, '_parent', parent)
        }
        let _level = 0
        if (level !== undefined && level !== null) {
            _level = level + 1
        }
        Vue.set(record, '_level', _level)
        tmp.push(record)
        if (record.child_dto_list && record.child_dto_list.length > 0) {
            // let indez = 0;
            Array.from(record.child_dto_list).forEach(function (recd_child,indez) {
                let len = record.child_dto_list.length;
                let sort = 0;   // 排序：0不排序，1(↑)，2(↓), 12(↑ ↓), 默认0
                if (len >= 2){
                    if( indez == 0 ) {
                        sort = 2;
                    }else if (indez == len - 1) {
                        sort = 1;
                    }else {
                        sort = 12
                    }
                }
                Vue.set(recd_child, '_sort', sort)
            })
            let child_dto_list = DataTransfer.treeToArray(record.child_dto_list, record, _level, expandedAll)
            tmp = tmp.concat(child_dto_list)
        }
    })
    return tmp
}

export default DataTransfer
